/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2017-8-29
*
\*********************************************************************/

#include <QSGTexture>
#include <QSGSimpleTextureNode>
#include <QQuickWindow>
#include <chrono>
#include <thread>

#include "OpenCVFrame.h"
#include "DisplayControl.h"
#include "UtilityTH.h"
#include "ImageDetect.h"
#include "V4l2Device.h"

#include <QThread>

OpenCVFrame::OpenCVFrame(QQuickItem *parent)
:QQuickItem(parent)
{
    setFlag(QQuickItem::ItemHasContents);
}

OpenCVFrame::~OpenCVFrame()
{
}

QSGNode* OpenCVFrame::updatePaintNode(QSGNode *old, UpdatePaintNodeData *)
{
    updateDataFrame();
    QSGSimpleTextureNode *texture = static_cast<QSGSimpleTextureNode*>(old);
    if (texture == NULL) {
        texture = new QSGSimpleTextureNode();
    }
    QSGTexture *t = window()->createTextureFromImage(m_imgFrame.scaled(boundingRect().size().toSize()));
    if (t) {
        QSGTexture *tt = texture->texture();
        if (tt) {
            tt->deleteLater();
        }
        texture->setRect(boundingRect());
        texture->setTexture(t);
    }
    return texture;
}

//void OpenCVShowFrame::updateDataFrame()
//{
//    static cv::Mat readmat = cv::imread("/home/root/pic.jpg");
//    m_imgFrame = MatToQImage(readmat);
//}


void OpenCVFrame::updateDataFrame()
{
//    lastime = nowtime;
//    nowtime = std::chrono::system_clock::now();
//    auto lastnowtime = std::chrono::duration_cast<std::chrono::microseconds>(nowtime - lastime);
//    qDebug() <<"lastnowtime" << static_cast<double>(lastnowtime.count())<< " us\n";

    cv::Mat matFrame;
    v4l2_buffer buf;

    auto ret = v4lDev.GetFrame(buf, matFrame);
    if(!ret)
    {
        qDebug() <<"GetFrame fail";
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
        return;
    }
    matFrame.convertTo(m_matFrame,-1,(float)GetContrast()/50);
    v4lDev.ReleaseBuf(buf);
    m_imgFrame = MatToQImage(m_matFrame);
}


//void OpenCVShowFrame::updateDataFrame()
//{
//    ImageDetect::GetInstance().m_dstImageMtx.lock();
//    ImageDetect::GetInstance().m_dstImage.copyTo(m_matFrame);
//    ImageDetect::GetInstance().m_dstImageMtx.unlock();
//    m_imgFrame = MatToQImage(m_matFrame);
////    imgFrame = MatToQImage(readmat);
//}


/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/
